package one.bugu.android.demon.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.juefeng.android.framework.LKUtil;
import com.juefeng.android.framework.view.annotation.LKContentView;
import com.juefeng.android.framework.view.annotation.LKInjectView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import one.bugu.android.demon.R;
import one.bugu.android.demon.bean.FarmBean;
import one.bugu.android.demon.bean.StateBean;
import one.bugu.android.demon.common.BaseHttpAsycResponceHandler;
import one.bugu.android.demon.common.MyBaseActivity;
import one.bugu.android.demon.constant.Constant;
import one.bugu.android.demon.constant.HttpConstant;
import one.bugu.android.demon.ui.activity.thirdparty.SpecialEventsActivity;
import one.bugu.android.demon.ui.widget.BaseTopBarView;
import one.bugu.android.demon.util.IntentUtils;
import one.bugu.android.demon.util.ParamUtils;
import one.bugu.android.demon.util.PreferencesUtil;
import one.bugu.android.demon.util.StatusBarUtils;

@LKContentView(R.layout.activity_task_center)
public class TaskCenterActivity extends MyBaseActivity implements View.OnClickListener {

    private String token;
    @LKInjectView(R.id.btbv_task_center)
    private BaseTopBarView baseTopBarView;

    @LKInjectView(R.id.new_regist)
    private TextView mNewTv;
    @LKInjectView(R.id.center_qianyiwan)
    private TextView mYiTv;
    @LKInjectView(R.id.wx_s)
    private TextView mWXTv;
    @LKInjectView(R.id.center_phone)
    private TextView mPhoneTv;
    @LKInjectView(R.id.add_cha)
    private LinearLayout mChLy; //添加客户
    @LKInjectView(R.id.add_phone)
    private LinearLayout mPhoneLy; //绑定手机
    @LKInjectView(R.id.center_signIn)
    private TextView mSignTv;
    @LKInjectView(R.id.center_in_ly)
    private LinearLayout mInLy; //邀请好友
    @LKInjectView(R.id.center_set_ly)
    private LinearLayout mSetLy; //设置预警
    @LKInjectView(R.id.center_miji)
    private TextView mMjTv; //算力秘籍
    @LKInjectView(R.id.center_setWarn)
    private TextView mSetWarnTv; //算力设置
    @LKInjectView(R.id.user_tel)
    private TextView mSuanliTv;
    /**
     * 玩游戏
     */
    @LKInjectView(R.id.ll_play_game)
    private LinearLayout llPlayGame;
    @LKInjectView(R.id.tv_play_game)
    private TextView tvPlayGame;
    @LKInjectView(R.id.tv_play_game_des)
    private TextView tvPlayGameDes;

    /**
     * 看小说
     */
    @LKInjectView(R.id.ll_look_book)
    private LinearLayout llLookBook;
    @LKInjectView(R.id.tv_look_book)
    private TextView tvLookBook;
    @LKInjectView(R.id.tv_look_book_des)
    private TextView tvLookBookDes;

    private int setWarn;
    //页面请求码
    public static final int REQUEST_CODE = 1001;

    @Override
    protected void initView() {
        super.initView();
        //设置状态栏
        StatusBarUtils.getInstance().setStatusBar("#5856D0", this, baseTopBarView, true);

        token = PreferencesUtil.getInstance().getString(TaskCenterActivity.this, Constant.TOKEN);

    }

    @Override
    protected void initData() {
        super.initData();
        getRegist();
        getSuanli();
    }

    private void getSuanli() {
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        LKUtil.getHttpManager().postBody(HttpConstant.GET_FARM_URL, map, Slhandler);
    }

    /**
     * 获取算力信息的网络请求
     */
    @SuppressLint("HandlerLeak")
    public BaseHttpAsycResponceHandler<FarmBean> Slhandler = new BaseHttpAsycResponceHandler<FarmBean>(true) {
        @Override
        public void onSuccess(FarmBean calculationBeans) {
            super.onSuccess(calculationBeans);
            mSuanliTv.setText((int) (calculationBeans.getPower()) + "");
        }

        @Override
        public void onFailed(int code, String message) {
            super.onFailed(code, message);
        }

        @Override
        public void onError() {
            super.onError();
        }
    };


    @Override
    protected void initEvent() {
        super.initEvent();

        baseTopBarView.setEventInterface(new BaseTopBarView.EventInterface() {
            @Override
            public void leftOnClick() {
                finish();
            }

            @Override
            public void leftCloseClick() {

            }

            @Override
            public void rightOnClick() {

            }
        });
        mChLy.setOnClickListener(this);
        mPhoneLy.setOnClickListener(this);
        mInLy.setOnClickListener(this);
        mSetLy.setOnClickListener(this);
        mMjTv.setOnClickListener(this);
        llPlayGame.setOnClickListener(this);
        llLookBook.setOnClickListener(this);
    }

    private void getRegist() {
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        LKUtil.getHttpManager().postBody(HttpConstant.GETTASKSTATE_URL, map, handler);
    }

    @SuppressLint("HandlerLeak")
    public BaseHttpAsycResponceHandler<StateBean> handler = new BaseHttpAsycResponceHandler<StateBean>(true) {
        @SuppressLint("SetTextI18n")
        @Override
        public void onSuccess(StateBean bean) {
            super.onSuccess(bean);
            setWarn = bean.getSetWarn();
            if (bean.getNewAccount() == 0) {
                mNewTv.setVisibility(View.INVISIBLE);
            } else {
                mNewTv.setVisibility(View.VISIBLE);
            }
            if (bean.getFirstAccount() == 0) {
                mYiTv.setVisibility(View.INVISIBLE);
            } else {
                mYiTv.setVisibility(View.VISIBLE);
            }
            if (bean.getAddServiceWatch() == 0) {
                mWXTv.setVisibility(View.INVISIBLE);
            } else {
                mWXTv.setVisibility(View.VISIBLE);
            }
            if (bean.getGongZhongHao() == 0) {
                mPhoneTv.setVisibility(View.INVISIBLE);
            } else {
                mPhoneTv.setVisibility(View.VISIBLE);
            }
            if (bean.getSignIn() == 0) {
                mSignTv.setVisibility(View.INVISIBLE);
            } else {
                mSignTv.setVisibility(View.VISIBLE);
            }

            if (bean.getSetWarn() == 0) {
                Drawable nav_up = getResources().getDrawable(R.mipmap.my_jiantou);
                nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
                mSetWarnTv.setCompoundDrawables(null, null, nav_up, null);
                mSetWarnTv.setText("     ");
            } else {
                Drawable nav_up = getResources().getDrawable(R.mipmap.renwu_wancheng);
                nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
                mSetWarnTv.setCompoundDrawables(null, nav_up, null, null);
                mSetWarnTv.setText("已完成");
            }

            //小说, 游戏领取算力数据
            tvLookBook.setText(bean.getFictionNum() + "/" + bean.getMaxNum());
            tvLookBookDes.setText("每日看小说轻松得算力\n每日上限" + bean.getMaxNum() + "个");

            tvPlayGame.setText(bean.getGameNum() + "/" + bean.getMaxNum());
            tvPlayGameDes.setText("每日玩游戏轻松得算力\n每日上限" + bean.getMaxNum() + "个");
        }

        @Override
        public void onFailed(int code, String message) {
            super.onFailed(code, message);
        }

        @Override
        public void onError() {
            super.onError();
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_cha:
                List<ParamUtils.NameValue> parList1 = ParamUtils.build().put(WebActivity.TITLE, "添加客服微信").put(WebActivity.URL, HttpConstant.ADD_WEIXIN_URL).create();
                IntentUtils.startAty(TaskCenterActivity.this, WebActivity.class, parList1);
                break;
            case R.id.add_phone:
                List<ParamUtils.NameValue> parList = ParamUtils.build().put(WebActivity.TITLE, "关注公众号并绑定手机").put(WebActivity.URL, HttpConstant.WEIXIN_URL).create();
                IntentUtils.startAty(TaskCenterActivity.this, WebActivity.class, parList);
                break;
            case R.id.center_in_ly:
                List<ParamUtils.NameValue> parList3 = ParamUtils.build().put(WebActivity.TITLE, "邀请好友").put(WebActivity.URL, HttpConstant.INVID_URL).create();
                IntentUtils.startAty(this, WebActivity.class, parList3);
                break;
            case R.id.center_set_ly:
                if (setWarn == 0) {
                    IntentUtils.startAty(this, WarnActivity.class);
                }
                break;
            case R.id.center_miji:
                List<ParamUtils.NameValue> parList2 = ParamUtils.build().put(WebActivity.TITLE, "算力秘籍").put(WebActivity.URL, HttpConstant.SUANLI_URL).create();
                IntentUtils.startAty(TaskCenterActivity.this, WebActivity.class, parList2);
                break;
            case R.id.ll_play_game:
                //玩游戏
                List<ParamUtils.NameValue> values = ParamUtils.build().put(SpecialEventsActivity.PAGE_TITLE, SpecialEventsActivity.GAME_TITLE).create();
                IntentUtils.startAtyForResult(this, SpecialEventsActivity.class, values, REQUEST_CODE);
                break;
            case R.id.ll_look_book:
                //看小说
                List<ParamUtils.NameValue> value = ParamUtils.build().put(SpecialEventsActivity.PAGE_TITLE, SpecialEventsActivity.BOOK_TITLE).create();
                IntentUtils.startAtyForResult(this, SpecialEventsActivity.class, value, REQUEST_CODE);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
            //刷新页面数据, 更新活动
            getRegist();
        }
    }
}